<template>
  <div class="warp_auto">
    <div class="warp_top">
      <div class="jiansuo-box">
        <span class="label">管路名称：</span>
        <el-input
          v-model="name"
          class="input"
          placeholder="请输入管路名称"
        />
      </div>
      <el-button
        type="primary"
        icon="el-icon-search"
        class="btn"
        @click="btn"
      >检索
      </el-button>
      <el-button
        type="primary"
        class="btn"
        icon="el-icon-plus"
        @click="(dialogFormVisibleAdd = true), openAdd()"
      >新增
      </el-button>
    </div>

    <!--模板（表格）-->
    <el-card>
      <el-table
        :data="arrlist"
        style="width: 100%"
        height="540"
        stripe
        :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
      >
        <el-table-column prop="tubeDrawingName" label="管路名称"></el-table-column>
        <el-table-column prop="planFlag" label="是否计划插拔管">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
                scope.row.planFlag == 1 ? "是" : scope.row.planFlag == 2 ? "否" : ""
              }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="intubationTime" label="插管时间">
        </el-table-column>
        <el-table-column prop="tubeDrawingTime" label="拔管时间">
        </el-table-column>

        <el-table-column prop="depth" label="插管深度"></el-table-column>
        <el-table-column prop="describeContent" label="插管描述">
        </el-table-column>
        <el-table-column prop="intubationExecutor" label="插管执行人">
        </el-table-column>
        <el-table-column prop="tubeDrawingExecutor" label="拔管执行人">
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          label="操作"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="openUpdate(scope, scope.$index)"
            >编辑
            </el-button>

            <el-popover
              :ref="`popover-${scope.$index}`"
              trigger="click"
              placement="top"
              width="100"
            >
              <p class="el-icon-warning">确定删除</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="mini"
                  type="text"
                  @click="no(scope, scope.$index)"
                >取消
                </el-button
                >
                <el-button
                  type="primary"
                  size="mini"
                  @click="yes(scope, scope.$index)"
                >确定
                </el-button
                >
              </div>
              <el-button
                slot="reference"
                size="mini"
                style="margin-left: 10px"
                type="danger"
              >删除
              </el-button
              >
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="paging">
        <el-pagination
          :current-page="start"
          :page-size="PageSize"
          :pager-count="5"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <el-card style="margin-top: 20px">
      <div slot="header" >管路甘特图</div>
      <!-- Echarts 图表 -->
      <div ref="progressChart" class="progressChart"></div>
    </el-card>

    <!-- 管理管理新增 -->
    <el-dialog
      title="新增"
      :visible.sync="dialogFormVisibleAdd"
    >
      <el-form ref="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-form-item label="管路名称：" prop="tubeDrawingName">
          <el-input v-model="info.tubeDrawingName" class="el-inputer"/>
        </el-form-item>
        <el-form-item label="是否计划插拔管：" prop="planFlag">
          <el-select v-model="info.planFlag">
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="插管时间:">
          <el-date-picker
            v-model="info.intubationTime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拔管时间：">
          <el-date-picker
            v-model="info.tubeDrawingTime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="插管深度：" prop="depth">
          <el-input v-model="info.depth" class="el-inputer"/>
        </el-form-item>
        <el-form-item label="插管描述：" prop="describeContent">
          <el-input v-model="info.describeContent" class="el-inputer"/>
        </el-form-item>


        <el-form-item label="插管执行人：" prop="intubationExecutor">
          <el-select v-model="info.intubationExecutor">
            <el-option
              v-for="item in arrUserName"
              :key="item.id"
              :label="item.user_name"
              :value="item.user_name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拔管执行人：" prop="tubeDrawingExecutor">
          <el-select v-model="info.tubeDrawingExecutor">
            <el-option
              v-for="item in arrUserName"
              :key="item.id"
              :label="item.user_name"
              :value="item.user_name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitFormAdd"
        >保 存
        </el-button
        >
      </div>
    </el-dialog>
    <!-- 管理管理编辑 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogFormVisibleUpdate"
      class="dialogClass"
      :close-on-click-modal="false"
      custom-class="role-mask"
    >
      <el-form ref="ruleForm" label-width="150px" class="demo-ruleForm">
        <el-form-item label="管路名称：" prop="tubeDrawingName">
          <el-input v-model="info.tubeDrawingName" class="el-inputer"/>
        </el-form-item>
        <el-form-item label="是否计划插拔管：" prop="planFlag">
          <el-select v-model="info.planFlag">
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="插管时间：">
          <el-date-picker
            v-model="info.intubationTime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拔管时间：">
          <el-date-picker
            v-model="info.tubeDrawingTime"
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="插管深度：" prop="depth">
          <el-input v-model="info.depth"/>
        </el-form-item>
        <el-form-item label="插管描述：" prop="describeContent">
          <el-input v-model="info.describeContent"/>
        </el-form-item>
        <el-form-item label="插管执行人：" prop="intubationExecutor">
          <el-select v-model="info.intubationExecutor">
            <el-option
              v-for="item in arrUserName"
              :key="item.id"
              :label="item.user_name"
              :value="item.user_name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拔管执行人：" prop="tubeDrawingExecutor">
          <el-select v-model="info.tubeDrawingExecutor">
            <el-option
              v-for="item in arrUserName"
              :key="item.id"
              :label="item.user_name"
              :value="item.user_name"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleUpdate = false">取 消</el-button>
        <el-button
          :plain="true"
          type="primary"
          @click="submitFormUpdate"
        >保 存
        </el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import {
  IntubationrecordqueryList,
  Intubationrecordadd,
  Intubationrecordupdate,
  IntubationrecorddeleteData,
  getUserListByUserType,
} from '@/api/table'

export default {
  data() {
    return {
      arrlist: [],
      data: {},
      name: "",
      arrUserName: [],
      patientId: "",
      dialogFormVisibleAdd: false,//添加页面
      dialogFormVisibleUpdate: false,//修改页面
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意 : 这里不能为空)
      totalCounts: 0,
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10, // 默认每页显示的条数（可修改）
      info: {
        id: "",
        patientId: "",
        intubationTime: "",
        tubeDrawingTime: "",
        describeContent: "",
        depth: "",
        intubationExecutor: "",
        tubeDrawingExecutor: "",
        tubeDrawingName: "",
        planFlag: "",
      },
      statusList: [
        {
          id: "0",
          value: 1,
          label: "是",
        },
        {
          id: "1",
          value: 2,
          label: "否",
        },
      ],
      beginTimeList: [],
      endTimeList: [],
      nameList: [],
    }
  },
  created() {
    this.data = this.$store.state.user.severFall;

    if (typeof this.data.patient_id == "undefined") {
      alert("请选择一次病人");
    } else {

      getUserListByUserType().then((res) => {
        this.arrUserName = res.data;
      });

      this.patientId = this.data.patient_id;
      this.pipelineQueryList(this.PageSize, this.start);
    }
  },
  methods: {
    /* 初始化图表 */
    initChart() {
      const progressChart = echarts.init(this.$refs.progressChart)
      const option = {
        // 鼠标移入提示工具
        tooltip: {
          trigger: 'axis',
          formatter(params) {
            if (params[1].data && params[0].data) {
              return `<div>开始时间：${params[1].data}</div>` + `<div>结束时间：${params[0].data}</div>`
            } else {
              return ''
            }
          },
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          containLabel: true,
          show: false,
          right: 80,
          left: 40,
          bottom: 40,
          top: 20,
          backgroundColor: '#fff'
        },
        legend: {
          // 图例组件
          data: ['持续时间'],
          align: 'auto'
        },
        xAxis: {
          type: 'time',
          axisTick: {
            // 隐藏刻度
            show: false
          },
          axisLine: {
            // 隐藏轴线
            show: false
          },
          splitLine: {
            // 显示网格线
            show: true
          }
        },
        yAxis: {
          inverse: true, // y 轴数据翻转，该操作是为了保证项目一放在最上面，项目七在最下面
          axisTick: {
            // 隐藏刻度
            show: false
          },
          axisLine: {
            // 隐藏轴线
            show: false
          },
          data: this.nameList
          // data: ['项目一', '项目二', '项目三', '项目四', '项目五', '项目六', '项目七']
        },
        series: [
          {
            name: '持续时间',
            type: 'bar',
            stack: 'duration',
            itemStyle: {
              color: '#007acc',
              borderColor: '#fff',
              borderWidth: 1
            },
            zlevel: -1,
            data: this.endTimeList
            // data: ['2021-01-31', '2021-02-25', '2021-03-25', '2021-04-01', '2021-04-10', '2021-05-25', '2021-07-25'] // 结束时间
          },
          {
            name: '持续时间',
            type: 'bar',
            stack: 'duration', // 堆叠标识符，同个类目轴上系列配置相同的 stack 值可以堆叠放置
            itemStyle: {
              color: '#fff'
            },
            zlevel: -1, // zlevel 大的 Canvas 会放在 zlevel 小的 Canvas 的上面
            z: 9, // z值小的图形会被z值大的图形覆盖，z相比zlevel优先级更低，而且不会创建新的 Canvas
            data: this.beginTimeList
            // data: ['2021-01-01', '2021-01-21', '2021-02-25', '2021-03-25', '2021-04-01', '2021-04-10', '2021-05-25'] // 开始时间
          }
        ]
      }
      progressChart.setOption(option)
      // 浏览器窗口大小变化，图表大小自适应
      window.addEventListener('resize', () => {
        progressChart.resize()
      })
    },

// 搜索按钮
    btn() {
      this.start = 1;
      this.pipelineQueryList(this.PageSize, this.start)
    },
//手术信息检索
    pipelineQueryList(pageSize, start) {
      IntubationrecordqueryList({
        size: pageSize,
        start: start,
        patientId: this.patientId,
        name: this.name,
        status: 1
      }).then((res) => {
        this.arrlist = res.data;

        if(this.arrlist.length != 0){
          this.beginTimeList = [];
          this.endTimeList = [];
          this.nameList = [];

          this.arrlist.forEach(item => {
            this.beginTimeList.push(item.intubationTime);
            this.endTimeList.push(item.tubeDrawingTime);
            this.nameList.push(item.tubeDrawingName);
          });

          setTimeout(() => {
            this.initChart();
          }, 500)
        }

        this.totalCounts = res.totalCount;
      });
    }
    ,
    // 每页显示的条数
    handleSizeChange(val) {
      this.PageSize = val
      this.start = 1
      this.pipelineQueryList(this.PageSize, this.start) // 模板
    }
    ,
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.start = val
      //先判断开关再判断请求
      this.pipelineQueryList(this.PageSize, this.start) // 模板
    },
    //打开添加页面
    openAdd() {
      this.info = {
        id: "",
        patientId: "",
        intubationTime: "",
        tubeDrawingTime: "",
        describeContent: "",
        depth: "",
        intubationExecutor: "",
        tubeDrawingExecutor: "",
        tubeDrawingName: "",
        planFlag: "",
      }

      this.dialogFormVisibleAdd = true;
    }
    ,
    //打开修改页面
    openUpdate(scope, index) {
      console.log(scope.row)
      this.dialogFormVisibleUpdate = true;
      this.info.patientId = this.patientId;
      this.info.intubationTime = scope.row.intubationTime,
        this.info.tubeDrawingTime = scope.row.tubeDrawingTime,
        this.info.describeContent = scope.row.describeContent,
        this.info.depth = scope.row.depth,
        this.info.intubationExecutor = scope.row.intubationExecutor,
        this.info.tubeDrawingExecutor = scope.row.tubeDrawingExecutor,
        this.info.tubeDrawingName = scope.row.tubeDrawingName,
        this.info.planFlag = scope.row.planFlag,
        this.info.id = scope.row.id;
    }
    ,
    //添加
    submitFormAdd() {
      Intubationrecordadd({
        patientId: this.patientId,
        intubationTime: this.info.intubationTime,
        tubeDrawingTime: this.info.tubeDrawingTime,
        describeContent: this.info.describeContent,
        depth: this.info.depth,
        intubationExecutor: this.info.intubationExecutor,
        tubeDrawingExecutor: this.info.tubeDrawingExecutor,
        tubeDrawingName: this.info.tubeDrawingName,
        planFlag: this.info.planFlag == "" ? "" : Number(this.info.planFlag),
      }).then((res) => {
        this.dialogFormVisibleAdd = false;
        this.pipelineQueryList(this.PageSize, 1);
      });
    }
    ,
    //修改
    submitFormUpdate() {
      Intubationrecordupdate({
        patientId: this.patientId,
        intubationTime: this.info.intubationTime,
        tubeDrawingTime: this.info.tubeDrawingTime,
        describeContent: this.info.describeContent,
        depth: this.info.depth,
        intubationExecutor: this.info.intubationExecutor,
        tubeDrawingExecutor: this.info.tubeDrawingExecutor,
        tubeDrawingName: this.info.tubeDrawingName,
        planFlag: this.info.planFlag == "" ? "" : Number(this.info.planFlag),
        status: 1,
        id: this.info.id,
      }).then((res) => {
        this.dialogFormVisibleUpdate = false;
        this.pipelineQueryList(this.PageSize, 1);
      });
    }
    ,
    //删除
    yes(scope, index) {
      // 可以在这里执行删除数据的回调操作.......删除操作......
      IntubationrecorddeleteData({id: scope.row.id}).then((res) => {
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.pipelineQueryList(this.PageSize, 1);
      });
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    }
    ,
    no(scope) {
      this.$message('取消删除')
      scope._self.$refs[`popover-${scope.$index}`].doClose()
    }
    ,
  }
}
</script>

<style lang="scss" scoped>
.progressChart {
  width: 100%;
  height: 500px;
}
</style>
